package home1ess.passport.controller;


import common.web.JsonResult;
import home1ess.passport.pojo.param.UserLoginParam;
import home1ess.passport.pojo.param.UserRegParam;
import home1ess.passport.pojo.param.UserUpdatePasswordParam;
import home1ess.passport.pojo.vo.UserLoginVO;
import home1ess.passport.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

import static common.web.StatusCode.BAD_REQUEST;

@Slf4j
@Api(tags = "登录服务")
@RestController
@RequestMapping("/passport")
@Validated
public class UserController {

    @Autowired
    private IUserService userService;

    @PostMapping("/reg")
    @ApiOperation("注册")
    public JsonResult reg (@Validated UserRegParam userRegParam) {
        //往我们的用户表插入一条记录
        userService.reg(userRegParam);
        return JsonResult.ok();
    }
    @PostMapping("/login")
    @ApiOperation("登录")
    public JsonResult login (@Validated UserLoginParam userLoginParam, HttpServletRequest request) {
        log.debug("userLoginParam:{}", userLoginParam);
        UserLoginVO userLoginVO = userService.login(userLoginParam, request);

        return JsonResult.ok(userLoginVO);
    }

    @PostMapping("/updatePassword")
    @ApiOperation("密码修改")
    public JsonResult updatePassword ( @Validated UserUpdatePasswordParam userUpdatePasswordParam) {
        System.out.println("userUpdatePassword:"+ userUpdatePasswordParam);
         userService.updatePassword(userUpdatePasswordParam);
        System.out.println("userUpdatePassword:"+ userUpdatePasswordParam);
        return JsonResult.ok();
    }
}
